import request from '@/utils/axios'

export type CronJobType = {
  id: number | undefined
  name: string
  status: number
  handlerName: string
  handlerParam: string
  cronExpression: string
  retryCount: number
  retryInterval: number
  monitorTimeout: number
  remark: string
  create_time: Date
}

export interface CronJobPageReqType extends PageParam {
  name?: string
  status?: number
  handlerName?: string
}

export type CronJobLogType = {
  id: number | undefined
  jobId: number
  handlerName: string
  handlerParam: string
  cronExpression: string
  executeIndex: string
  beginTime: Date
  endTime: Date
  duration: string
  status: number
  result: string
  create_time: Date
}

export interface CronJobLogPageReqType extends PageParam {
  jobId?: number | null
  handlerName?: string
  status?: number
  beginTime?: Date[]
}

/**
 * 定时任务相关接口
 */
export const CronJobApi = {
  // 新增
  create: (data: CronJobType) => {
    return request.post({ url: '/infra/cron-job/create', data, is_mock: true })
  },
  // 修改
  update: (data: CronJobType) => {
    return request.put({ url: '/infra/cron-job/update', data, is_mock: true })
  },
  // 修改状态
  updateStatus: (id: number, status: number) => {
    const data = {
      id,
      status
    }
    return request.put({ url: '/infra/cron-job/update-status', data, is_mock: true })
  },
  // 立即执行一次
  run: (id: number) => {
    return request.put({ url: '/infra/cron-job/trigger?id=' + id, is_mock: true })
  },
  // 删除
  delete: (id: number) => {
    return request.delete({ url: '/infra/cron-job/delete?id=' + id, is_mock: true })
  },
  // 查询详情
  detail: (id: number) => {
    return request.get({ url: '/infra/cron-job/detail?id=' + id, is_mock: true })
  },
  // 查询分页列表
  page: (params: CronJobPageReqType) => {
    return request.get({ url: '/infra/cron-job/page', params, is_mock: true })
  },
  // 获得定时任务的下 n 次执行时间
  nextTimes: (id: number) => {
    return request.get({ url: '/infra/cron-job/get-next-times?id=' + id, is_mock: true })
  },
  // 导出
  export: (params: CronJobPageReqType) => {
    return request.get({ url: '/infra/cron-job/export', params, is_mock: true })
  },
  // 调度日志
  Log: {
    // 查询详情
    detail: (id: number) => {
      return request.get({ url: '/infra/cron-job-log/detail?id=' + id, is_mock: true })
    },
    // 查询分页列表
    page: (params: CronJobLogPageReqType) => {
      return request.get({ url: '/infra/cron-job-log/page', params, is_mock: true })
    },
    // 导出
    export: (params: CronJobLogPageReqType) => {
      return request.get({ url: '/infra/cron-job-log/export', params, is_mock: true })
    }
  }
}
